package com.fa4j.system.notice.controller;

import com.fa4j.common.base.exception.CommonRequestError;
import com.fa4j.common.base.model.Page;
import com.fa4j.common.base.model.RemoveCmd;
import com.fa4j.common.web.annotation.ApiMethod;
import com.fa4j.common.web.model.AppType;
import com.fa4j.system.login.util.LoginUtil;
import com.fa4j.system.notice.model.Notice;
import com.fa4j.system.notice.model.NoticeCreateCmd;
import com.fa4j.system.notice.model.NoticeModifyStateCmd;
import com.fa4j.system.notice.model.NoticeQuery;
import com.fa4j.system.notice.repository.NoticeRepository;
import com.fa4j.system.notice.service.NoticeService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("system/notice")
@RequiredArgsConstructor
public class NoticeController {

    private final NoticeService noticeService;
    private final NoticeRepository noticeRepository;

    @ApiMethod(name = "系统通知-分页查询", type = AppType.MANAGE, auth = ApiMethod.Switch.OFF)
    @GetMapping("page")
    public Page<Notice> page(NoticeQuery query) {
        query.setAccountId(LoginUtil.getAccountId());
        return noticeRepository.queryPage(query);
    }

    @ApiMethod(name = "系统通知-详情查询", type = AppType.MANAGE, auth = ApiMethod.Switch.OFF)
    @GetMapping("{id}")
    public Notice detail(@PathVariable Long id) {
        return noticeRepository.findById(id)
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("系统通知"));
    }

    @ApiMethod(name = "系统通知-创建", type = AppType.MANAGE)
    @PostMapping
    public Notice create(@Valid @RequestBody NoticeCreateCmd cmd) {
        return noticeService.create(cmd);
    }

    @ApiMethod(name = "系统通知-修改状态", type = AppType.MANAGE, auth = ApiMethod.Switch.OFF)
    @PutMapping("modifyState")
    public Notice modifyState(@Valid @RequestBody NoticeModifyStateCmd cmd) {
        return noticeService.modifyState(cmd);
    }

    @ApiMethod(name = "系统通知-删除", type = AppType.MANAGE)
    @DeleteMapping("{id}")
    public Notice remove(@PathVariable Long id) {
        return noticeService.remove(RemoveCmd.of(id, LoginUtil.getAccountId()));
    }

}
